package com.zoweunion.mechanic.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.controller.BaseController;

import com.zoweunion.mechanic.dao.FacilitatorPersonnelRelationDao;
import com.zoweunion.mechanic.dao.app.ToolCarDataMapper;
import com.zoweunion.mechanic.dao.base.MessageRecordDao;
import com.zoweunion.mechanic.dao.base.OwnerDao;
import com.zoweunion.mechanic.entity.FaultCodeBean;
import com.zoweunion.mechanic.entity.RenewalCardEntity;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.plugins.shiro.JWTUtil;
import com.zoweunion.mechanic.service.app.ToolCarDataService;
import com.zoweunion.mechanic.service.base.UserService;
import com.zoweunion.mechanic.util.ExcelUtils;
import com.zoweunion.mechanic.util.Orgin;
import com.zoweunion.mechanic.util.UuidUtil;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.task.CarDataAbnormalTask;
import com.zoweunion.mechanic.util.upload.UploadFile;
import com.zoweunion.mechanic.util.upload.UploadResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@EnableScheduling
@RequestMapping("/tool/car")
public class ToolCarDataController extends BaseController {

    @Autowired
    private ToolCarDataService carDataService;

    @Autowired
    private ToolCarDataMapper toolCarDataMapper;

    @Autowired
    private CarDataAbnormalTask carDataAbnormalTask;

    @Autowired
    private UserService userService;

    @Autowired
    private OwnerDao ownerDao;

    @Autowired
    private FacilitatorPersonnelRelationDao facilitatorPersonnelRelationDao;

    /**
     * 工具 获取车辆列表
     * @param str
     * @param request
     * @return
     */
    @Deprecated
    @PostMapping("list")
    public ResponseBean list(@RequestBody String str, HttpServletRequest request){
        logBefore(logger, "工具 获取车辆列表");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        List<Map<String, Object>> carList = carDataService.list(currentUser, reqMap);
        logAfter(logger);

        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carList);
    }

    /**
     * 工具 获取车辆列表
     * @param str
     * @param request
     * @return
     */
    @PostMapping("listCarDateNew")
    public ResponseBean listCarDateNew(@RequestBody String str, HttpServletRequest request){
        logBefore(logger, "工具 获取车辆列表");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        List<Map<String, Object>> carList = carDataService.listCarDateNew(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carList);
    }

    /**
     * 工具 获取车辆新
     * @param str
     * @param request
     * @return
     */
    @GetMapping("listForCarDateNew")
    public ResponseBean listForCarDateNew(@RequestParam Map<String,Object> str, HttpServletRequest request){
        //民生银行需求需要从前端传服务商或者主机厂的用户账号
        List<Map<String, Object>> carList = new ArrayList<>();
//        if(str.get("s_id") != null && str.get("s_id") != "") {
//            Map<String, Object> serverMap = facilitatorPersonnelRelationDao.getFacilitatorInfo(str.get("s_id").toString());
//            User currentUser = userService.getUserByAccount(serverMap.get("user_account").toString());
//            carList = carDataService.listForCarDateNew(currentUser,str);
//        }else {
        carList = carDataService.listForCarDateNew(getCurrentUserNoVerify(request), str);
//        }
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carList);
    }

    /**
     * 工具 分享到第三方：获取车辆新
     * @param str
     * @return
     */
    @GetMapping("listForCarDateNewForThird")
    public ResponseBean listForCarDateNewForThird(@RequestParam Map<String,Object> str){
        List<Map<String, Object>> carList = new ArrayList<>();
        carList = carDataService.listForCarDateNewForThird(str);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carList);
    }

    /**
     * 工具 获取车辆新
     * @param str
     * @param request
     * @return
     */
    @GetMapping("listForCarPage")
    public ResponseBean listForCarPage(@RequestParam Map<String,Object> str, HttpServletRequest request){
        PageInfo carList = carDataService.listForCarPage(getCurrentUserNoVerify(request),str);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carList);
    }



    /**
     * 工具 获取车辆详情新
     * @param str
     * @param request
     * @return
     */
    @GetMapping("listCarDateInfoNew")
    public ResponseBean listCarDateInfoNew(@RequestParam Map<String,Object> str, HttpServletRequest request){
        logBefore(logger, "工具 获取车辆详情");
        // 获取当前用户的服务商id
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carList = carDataService.listCarDateInfoNew(currentUser, str);
        //返回服务商名称和总运行小时
        if (str.containsKey("sid")) {
            carList = carDataService.addName(carList, str);
        }
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carList);
    }

    /**
     * 工具 获取车辆实时数据
     * @param str
     * @param request
     * @return
     */
    @PostMapping("realData")
    public ResponseBean realData(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 获取车辆实时数据");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carData = carDataService.realData(currentUser, reqMap);

        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     *实时数据查看更多
     */
    @GetMapping("selectMoreData")
    public ResponseBean selectMoreData(@RequestParam Map<String,Object> map) throws MyException {

        Map<String, Object> moreData = carDataService.selectMoreData(map);

        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", moreData);
    }

    /**
     * 历史轨迹查看接口
     */
    @GetMapping("selectHistoryData")
    public Object selectHistoryData(@RequestParam Map<String,Object> map) throws MyException {
        ResponseBean data = carDataService.selectHistoryData(map);
        Map<String, Object> data1 = (Map<String, Object>) data.getData();
        List points = (List) data1.get("points");
        if (points == null || points.size() == 0) {
            map.put("type", 1);
            data = carDataService.selectHistoryDataByStartDateAndEndDate(map);
        }
        return data;
    }

    /**
     * 历史轨迹查看接口(增加了查询猎鹰的逻辑:1、先不纠偏查询的到包含自定义属性的坐标信息；2、再根据起止时间查询开启纠偏的纠正坐标点)
     */
    @GetMapping("selectHistoryDataNew")
    public Object selectHistoryDataNew(@RequestParam Map<String,Object> map) throws MyException {
        ResponseBean data = carDataService.selectHistoryDataNew(map);
        Map<String, Object> data1 = (Map<String, Object>) data.getData();
        List points = (List) data1.get("points");
        if (points == null || points.size() == 0) {
            map.put("type", 1);
            data = carDataService.selectHistoryDataNew(map);
        }
        return data;
    }

    /**
     * 无线设备数据
     * @param map
     */
    @GetMapping("selectWirelessDeviceData")
    public ResponseBean selectWirelessDeviceData(@RequestParam Map<String,Object> map) throws MyException {
        Map<String, Object> data = carDataService.selectWirelessDeviceData(map);
        return new ResponseBean(Constants.CODE_SUCCESS,"查询无线设备数据成功！",data);
    }


    /**
     * 查询有历史轨迹的日期集合接口
     */
    @GetMapping("selectHistoryDataList")
    public Object selectHistoryDataList(@RequestParam Map<String,Object> map) throws  ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cc = Calendar.getInstance();
        String startDateStr = sdf.format(sdf.parse(String.valueOf(map.get("startDate"))));
        String endDateStr = sdf.format(sdf.parse(String.valueOf(map.get("endDate"))));
        Date startDate = sdf.parse(startDateStr);
        Date endDate = sdf.parse(endDateStr);
        List list = new ArrayList<>();
        while (!startDate.after(endDate))
        {
            String startTime = sdf.format(startDate) + " 00:00:00";
            String endTime = sdf.format(startDate) + " 23:59:59";
            Map<String,Object> reqMap = new HashMap<>();
            reqMap.put("startTime", startTime);
            reqMap.put("endTime", endTime);
            reqMap.put("things_id", map.get("things_id"));
            ResponseBean data = carDataService.selectHistoryData(reqMap);
            Map<String, Object> data1 = (Map<String, Object>) data.getData();
            List points = (List) data1.get("points");
            if (points == null || points.size()==0) {
                reqMap.put("type", 1);
                data = carDataService.selectHistoryData(reqMap);
            }
            if (((List) ((Map<String, Object>) data.getData()).get("points")).size() != 0) {
                list.add(sdf.format(startDate));
            }
            cc.setTime(startDate);
            cc.add(cc.DATE, 1);
            startDate = cc.getTime();
        }
        return list;
//        ResponseBean data = carDataService.selectHistoryData(reqMap);
//        Map<String, Object> data1 = (Map<String, Object>) data.getData();
//        List points = (List) data1.get("points");
//        if (points.size()==0){
//            map.put("type",1);
//            data=carDataService.selectHistoryData(map);
//        }
//        return data;
    }


    /**
     * 轨迹数据、节点记录
     */
    @GetMapping("selectHistory")
    public Object selectHistory(@RequestParam Map<String,Object> map) throws MyException, ParseException {
        logBefore(logger, "轨迹数据、节点记录");
        List<Map<String, Object>> history = carDataService.selectHistory(map);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", history);
    }


    /**
     * 轨迹数据、节点记录
     */
    @GetMapping("selectTrackHistory")
    public Object selectTrackHistory(@RequestParam Map<String,Object> map) throws MyException, ParseException {
        logBefore(logger, "轨迹数据、节点记录");
        LinkedHashMap<String,List<Map<String, Object>>> history = carDataService.selectTrackHistoryLastNew(map);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", history);
    }

    /**
     * 轨迹数据、节点记录
     */
    @GetMapping("selectTrackHistoryOld")
    public Object selectTrackHistoryOld(@RequestParam Map<String,Object> map) throws MyException, ParseException {
        logBefore(logger, "轨迹数据、节点记录");
        List<Map<String, Object>> history = carDataService.selectTrackHistory(map);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", history);
    }

    /**
     * 工具 获取车辆实时数据
     * @param str
     * @param request
     * @return
     */
    @PostMapping("realDataNew")
    public ResponseBean realDataNew(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 获取车辆列表");
        // 获取当前用户的服务商id
        // 判断是服务商是灰熊动力
        String  sid =  getCurrentUser(request).getS_id();
        boolean isHxdl = false;
        if(Constants.hxdlSId.equals(sid) || Constants.hxdlSIdPid.equals(sid)){
            isHxdl = true;
        }

        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);

        Map<String, Object> carInfoRealDateMap = carDataService.realDataNew(reqMap);

        Map<String, Object> tempMap = new HashMap<>();

        tempMap.put("mapData",carInfoRealDateMap);
        tempMap.put("isHxdl",isHxdl);

        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", tempMap);
    }

    /**
     * 工具 车辆油位统计新
     * @param request
     * @return
     */
    @GetMapping("carOilLevelStatisticsNew")
    public ResponseBean carOilLevelStatisticsNew(@RequestParam Map<String,Object> map, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 车辆油位统计");
        // 获取当前用户的服务商id
        Map<String, Object> carData = carDataService.carOilLevelStatisticsNew(map);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 工具 油耗工时统计
     * @param request
     * @return
     */
    @GetMapping("carCountFuelWork")
    public ResponseBean carCountFuelWork(@RequestParam Map<String,Object> map, HttpServletRequest request) throws MyException {
        // 获取当前用户的服务商id
        Map<String, Object> carData = carDataService.carCountFuelWork(map);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 工具 获取车辆维修记录
     * @param str
     * @param request
     * @return
     */
    @PostMapping("maintenanceRecords")
    public ResponseBean maintenanceRecords(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 获取车辆维修记录");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        List<Map<String, Object>> carData = carDataService.maintenanceRecords(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }


    /**
     * 工具 获取车辆历史轨迹
     * @param str
     * @param request
     * @return
     */
    @PostMapping("historyTrajectory")
    public ResponseBean historyTrajectory(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 获取车辆历史轨迹");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        List<Map<String, Object>> carData = carDataService.historyTrajectory(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 工具 车辆工时统计
     * @param str
     * @param request
     * @return
     */
    @PostMapping("carWorkingHoursStatistics")
    public ResponseBean carWorkingHoursStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 车辆工时统计");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carData = carDataService.carWorkingHoursStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 工具 车辆油耗统计
     * @param str
     * @param request
     * @return
     */
    @Deprecated
    @PostMapping("carFuelConsumptionStatistics")
    public ResponseBean carFuelConsumptionStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 车辆油耗统计");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carData = carDataService.carFuelConsumptionStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 工具 车辆油耗工时统计
     * @param str
     * @param request
     * @return
     */
    @PostMapping("carFuelConsumptionWorkHoursStatistics")
    public ResponseBean carFuelConsumptionWorkHoursStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 车辆油耗工时统计");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carData = carDataService.carFuelConsumptionWorkHoursStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 工具 车辆油耗工时统计
     * @param str
     * @param request
     * @return
     */
    @PostMapping("carFuelConsumptionWorkHoursStatistics2")
    public ResponseBean carFuelConsumptionWorkHoursStatistics2(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 车辆油耗工时统计");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carData = carDataService.carFuelConsumptionWorkHoursStatistics2(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 工具 车辆油位统计
     * @param str
     * @param request
     * @return
     */
    @PostMapping("carOilLevelStatistics")
    public ResponseBean carOilLevelStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 车辆油位统计");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carData = carDataService.carOilLevelStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 获取工时分布统计
     * @param str
     * @param request
     * @return
     */
    @PostMapping("workingHoursDistributionStatistics")
    public ResponseBean workingHoursDistributionStatistics(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取工时分布统计");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carData = carDataService.workingHoursDistributionStatistics(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 获取天气信息
     * @param str
     * @param request
     * @return
     */
    @PostMapping("getWeather")
    public ResponseBean getWeather(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取天气信息");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carData = carDataService.getWeather(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 获取车辆油箱容量
     * @param str
     * @param request
     * @return
     */
    @PostMapping("getFuelTank")
    public ResponseBean getFuelTank(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取车辆油箱容量");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carData = carDataService.getFuelTank(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 更新车辆油箱容量
     * @param str
     * @param request
     * @return
     */
    @PostMapping("modifyFuelTank")
    public ResponseBean modifyFuelTank(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "更新车辆油箱容量");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        carDataService.modifyFuelTank(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }



    /**
     * 工具 车辆实时故障码
     * @param str
     * @param request
     * @return
     */
    @PostMapping("carRealFaultCode")
    public ResponseBean carRealFaultCode(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 车辆实时故障码");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carData = carDataService.carRealFaultCode(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 工具 车辆历史故障码
     * @param str
     * @param request
     * @return
     */
    @PostMapping("carHistoryFaultCode")
    public ResponseBean carHistoryFaultCode(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 车辆历史故障码");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carData = carDataService.carHistoryFaultCode(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carData);
    }

    /**
     * 工具 抓取历史故障代码
     * @param str
     * @param request
     * @return
     */
    @PostMapping("grabHistoryFaultCode")
    public ResponseBean grabHistoryFaultCode(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 抓取历史故障代码");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        carDataService.grabHistoryFaultCode(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }

    /**
     * 工具 删除今日数据中的历史故障代码
     * @param str
     * @param request
     * @return
     */
    @PostMapping("deleteTodayHistoryFaultCode")
    public ResponseBean deleteTodayHistoryFaultCode(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "工具 删除今日数据中的历史故障代码");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUserNoVerify(request);
        carDataService.deleteTodayHistoryFaultCode(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }

    /**
     * 保存车辆附件图片信息
     * @param str
     * @param request
     * @return
     */
    @PostMapping("saveCarAttachment")
    public ResponseBean saveCarAttachment(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "保存车辆附件图片信息");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, new TypeReference<Map<String, Object>>(){});
        User currentUser = getCurrentUserNoVerify(request);
        carDataService.saveCarAttachment(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }

    /**
     * 获取车辆附件图片信息
     * @param str
     * @param request
     * @return
     */
    @PostMapping("getCarAttachment")
    public ResponseBean getCarAttachment(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "保存车辆附件图片信息");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, new TypeReference<Map<String, Object>>(){});
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> carAttachment = carDataService.getCarAttachment(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carAttachment);
    }

    /**
     * 车辆成员组成
     * @param str
     * @param request
     * @return
     */
    @PostMapping("carMembership")
    public ResponseBean carMembership(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "车辆成员组成");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, new TypeReference<Map<String, Object>>(){});
        User currentUser = getCurrentUserNoVerify(request);
        List<Map<String, Object>> carList = carDataService.carMembership(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carList);
    }

    /**
     * 仪表盘数据展示
     * @param str
     * @param request
     * @return
     */
    @PostMapping("getDashboard")
    public ResponseBean getDashboard(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "仪表盘数据展示");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, new TypeReference<Map<String, Object>>(){});
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> data = carDataService.getDashboard(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", data);
    }

    /**
     * GPS卫星图
     * @param str
     * @param request
     * @return
     */
    @PostMapping("getGPSsatellite")
    public ResponseBean getGPSsatellite(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "GPS卫星图");
        // 获取当前用户的服务商id
        Map<String, Object> reqMap = JSONObject.parseObject(str, new TypeReference<Map<String, Object>>(){});
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> data = carDataService.getGPSsatellite(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", data);
    }

    /**
     * 查询所有物联网关
     * @param request
     * @return
     */
    @GetMapping("/selectPcCarList")
    public ResponseBean selectPcCarList(@RequestParam Map<String,Object> map,
                                        HttpServletRequest request) throws MyException, ParseException {

        User currentUser = getCurrentUser(request);
        map.put("s_id",currentUser.getS_id());
        List<String> byThingsId = carDataService.selectPcCarList(map);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", byThingsId);
    }

    /**
     * 根据物联网关返回字段值
     * @param request
     * @return
     */
    @GetMapping("/selectPcCarColumnList")
    public ResponseBean selectPcCarColumnList(@RequestParam Map<String,Object> map,
                                              HttpServletRequest request) throws MyException, ParseException {

        List<String> byThingsId = carDataService.selectPcCarColumnList(map);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", byThingsId);
    }

    /**
     * 根据thingid返回信息
     * @param request
     * @return
     */
    @GetMapping("/selectInfoByThingsId")
    public ResponseBean selectInfoByThingsId(@RequestParam Map<String,Object> map,
                                             HttpServletRequest request) throws MyException, ParseException {

        List<Map<String, Object>> byThingsId = carDataService.selectInfoByThingsId(map);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", byThingsId);
    }

    /**
     * 卡特外部接口
     */
    @GetMapping("/selectCarList")
    public ResponseBean selectCarList(@RequestParam Map<String,Object> map,
                                      HttpServletRequest request) throws MyException, ParseException {

        List<Map<String, Object>> list = carDataService.selectCarList(map);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", list);
    }

    /**
     * 工时统计(卡特当天实时查询)
     */
    @GetMapping("/selectWorkHourNew")
    public ResponseBean selectWorkHourNew(@RequestParam Map<String,Object> map,
                                          HttpServletRequest request) throws Exception {

        Map<String, Object>  list = carDataService.selectWorkHourNew(map);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", list);
    }

    /**
     * 临时刷历史数据
     */
    @GetMapping("/updateDateHistory")
    public ResponseBean updateDateHistory(@RequestParam Map<String,Object> map,
                                          HttpServletRequest request) throws Exception {

        carDataService.updateDateHistory();
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 卡特故障列表
     */
    @GetMapping("/selectCarFaultCode")
    public ResponseBean selectCarFaultCode(@RequestParam Map<String,Object> map,
                                           HttpServletRequest request) throws MyException, ParseException {

        PageInfo list = carDataService.selectCarFaultCode(map);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", list);
    }

    /**
     * 卡特故障详情
     */
    @GetMapping("/selectCarFaultCodeInfo")
    public ResponseBean selectCarFaultCodeInfo(@RequestParam Map<String,Object> map,
                                               HttpServletRequest request) throws MyException, ParseException {

        Map<String, Object> list = carDataService.selectCarFaultCodeInfo(map);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", list);
    }


    /**
     *历史数据根据模板设置
     */
    @GetMapping("selectMoreDataList")
    public ResponseBean selectMoreDataList(@RequestParam Map<String,Object> map) throws MyException {
        Map<String, Object> moreData = carDataService.selectMoreDataList(map);

        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", moreData);
    }


    /**
     *历史数据根据模板设置导出
     */
    @GetMapping("selectMoreDataListExport")
    public void selectMoreDataListExport(@RequestParam Map<String,Object> map, HttpServletResponse response) throws Exception {
        carDataService.selectMoreDataListExport(map,response);
    }


    /**
     *油位图片上传
     */
    @PostMapping(value="/uploadImg")
    public ResponseBean uploadImg(MultipartFile[] files , HttpServletRequest request) throws Exception {

        String uploadPath = "oilLevelPictureUrl";
        User currentUser = getCurrentUserNoVerify(request);
        Map<String, Object> map = new HashMap<>();
        map.put("type", request.getParameter("thingsId"));
        for (MultipartFile file : files) {
            UploadResult uploadImg = UploadFile.uploadImg(file, uploadPath, currentUser, map);
            if (uploadImg.getStatus().toString().equals("1")) {
                map.put("url", uploadImg.getFinalFileName());
                map.put("thumUrl", uploadImg.getThumFileName());
                map.put("createUser", currentUser.getId());
                map.put("photoTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(request.getParameter("photoTime")));
                carDataService.addOiLevelPictureUrl(map);
            }
        }
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", null);
    }


    /**
     *油位图片列表
     */
    @PostMapping(value="/oilLevelList")
    public Object oilLevelList(@RequestBody Map<String, Object> reqMap) {
        List<Map<String, Object>> pictureList = carDataService.getOilLevelList(reqMap);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", pictureList);
    }


    /**
     *油位图片删除
     */
    @PostMapping(value="/oilLevelDelete")
    public Object oilLevelDelete(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) {
        String updateUser = getCurrentUserNoVerify(request).getId();
        reqMap.put("updateUser", updateUser);
        carDataService.updateOilLevelUrl(reqMap);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", null);
    }

    /**
     * 故障代码列表
     * @param request
     * @return
     */
    @PostMapping("faultCodeList")
    public ResponseBean faultCodeList(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) {
        logBefore(logger, "故障代码列表");
        // 获取当前用户的服务商id
        User currentUser = getCurrentUserNoVerify(request);
        PageInfo faultCodeList = carDataService.faultCodeList(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", faultCodeList);
    }

    /**
     * 故障代码-条件查询
     * 主机厂、服务商查询旗下的所有服务商(包括自己）
     * @param request
     * @return
     */
    @PostMapping("getsidList")
    public ResponseBean getsidList(HttpServletRequest request) {
        logBefore(logger, "故障代码-条件查询");
        // 获取当前用户的服务商id
        User currentUser = getCurrentUserNoVerify(request);
        String sid = currentUser.getS_id();
        List<Map<String, Object>> sidList = toolCarDataMapper.getsidDetailList(sid);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", sidList);
    }

    /**
     * 某故障代码车辆发生情况
     * @param request
     * @return
     */
    @PostMapping("carFaultCodeList")
    public ResponseBean carFaultCodeList(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) {
        logBefore(logger, "某故障代码车辆发生情况");
        // 获取当前用户的服务商id
        User currentUser = getCurrentUserNoVerify(request);
        PageInfo carFaultCodeList = carDataService.carFaultCodeList(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carFaultCodeList);
    }

    /**
     * 某故障代码某车辆某次故障发生详情
     * @param request
     * @return
     */
    @PostMapping("carFaultDetailList")
    public ResponseBean carFaultDetailList(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) {
        logBefore(logger, "某故障代码某车辆某次故障发生详情");
        // 获取当前用户的服务商id
        User currentUser = getCurrentUserNoVerify(request);
        PageInfo carFaultDetailList = carDataService.carFaultDetailList(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", carFaultDetailList);
    }

    /**
     * 导出-故障代码列表
     *
     * */
    @GetMapping("/exportFaultCodeList")
    public void exportFaultCodeList(@RequestParam Map<String, Object> reqMap, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logBefore(logger, "导出-故障代码列表");
        // 获取当前用户的服务商id
        String user_account = JWTUtil.getUsername(reqMap.get("authorization").toString());
        User currentUser = userService.getUserByAccount(user_account);
        List<FaultCodeBean> exportFaultCodeList = carDataService.exportFaultCodeList(currentUser, reqMap);
        logAfter(logger);
        ExcelUtils.exportExcelToTarget(response, "故障代码", exportFaultCodeList, FaultCodeBean.class);
    }

    /**
     * 根据things_id获取对应的车辆的胎压信息
     * @param reqMap
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/carTirepressureData")
    public ResponseBean carTirepressureData(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) throws Exception {
        logBefore(logger, "获取车辆的胎压、温度等信息");
        List<Map<String, Object>> list = carDataService.carTirepressureData(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", list);
    }

    //机型对比推送 36  暂时注释掉，不懂先人写的啥

    //TODO  早上10点到晚上10点之间推送
    @PostMapping("/modelComparisonPush")
    public ResponseBean pushModelComparison(@RequestBody String str, HttpServletRequest request) throws Exception{
        String authorization = request.getHeader("Authorization");
        String user_name = JWTUtil.getUsername(authorization);
        User currentUser = userService.getUserByAccount(user_name);
        String mobile_phone = currentUser.getMobile_phone();

        Map<String, Object> str1 = JSON.parseObject(str);
        List<String> carModelIds = Arrays.asList(str1.get("carModelIds").toString().split(","));

//        if(carModelIds.size() > 0) {
//            String model_name = toolCarDataMapper.selectModelNameByCarModelId(carModelIds);
//            List<String> mapList =  toolCarDataMapper.selectBrandAndModel(carModelIds);
//            if (mapList.size() > 0) {
//                    for (String userId : mapList) {
//                        carDataAbnormalTask.jPushModelComparison(userId, currentUser.getUser_name(), mobile_phone, carModelIds.size(), model_name);
//                    }
//
//            }
//
////            List<Map<String, Object>> mapList = toolCarDataMapper.selectSidByCarModelId(carModelIds);
////            Set<String> sids = new HashSet<>();
////            if (mapList.size() > 0) {
////                mapList.forEach((item) -> sids.add(String.valueOf(item.get("s_id"))));
////                Set<Map<String, Object>> mapSets = toolCarDataMapper.selectUseridsBySids(sids);
////                if (mapSets. size() > 0) {
////                    for (Map<String, Object> item : mapSets) {
////                        carDataAbnormalTask.jPushModelComparison(item, item.get("id").toString(), currentUser.getUser_name(), mobile_phone, carModelIds.size(), model_name);
////                    }
////                }
////            }
//        }

        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", null);

    }

    //新机、二手机浏览询价推送  37   暂时注释掉，不懂先人写的啥
    //TODO  早上10点到晚上10点之间推送
//    @PostMapping("/newOrOldMachineBrowsePush")
//    public ResponseBean pushNewOrOldMachineBrowse(@RequestBody String str, HttpServletRequest request) throws Exception{
//        String authorization = request.getHeader("Authorization");
//        String user_name = JWTUtil.getUsername(authorization);
//        User currentUser = userService.getUserByAccount(user_name);
//        String mobile_phone = currentUser.getMobile_phone();
//
//        Map<String, Object> str1 = JSON.parseObject(str);
//        String carTypeId = str1.get("modelId").toString();
//        if(carTypeId != null) {
//            String type_name = toolCarDataMapper.selectModelNameByCarTypeId(carTypeId);
//
//            List<Map<String, Object>> mapList = toolCarDataMapper.selectSidByCarTypeId(carTypeId);
//            Set<String> sids = new HashSet<>();
//            if (mapList.size() > 0) {
//                mapList.forEach((item) -> sids.add(String.valueOf(item.get("s_id"))));
//                Set<Map<String, Object>> mapSets = toolCarDataMapper.selectUseridsBySids(sids);
//
//                if (mapSets.size() > 0) {
//                    for (Map<String, Object> item : mapSets) {
//                        carDataAbnormalTask.jPushNewOrOldMachineBrowse(item, item.get("id").toString(), user_name, mobile_phone, type_name);
//                    }
//                }
//            }
//        }
//        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", null);
//    }

    @PostMapping("/checkIsBindCar")
    public ResponseBean checkIsBindCar(@RequestParam String id, HttpServletRequest request)throws Exception{
        int count = ownerDao.countCarsByOid(id);
        if (count > 0){
            return new ResponseBean(Constants.CODE_SUCCESS,"已绑定车辆！",false);
        }else{
            return new ResponseBean(Constants.CODE_SUCCESS,"未绑定车辆！",true);
        }
    }

    @PostMapping("/checkThingsIdIsBind")
    public ResponseBean checkThingsIdIsBind(@RequestBody Map<String,Object> paramsMap, HttpServletRequest request)throws Exception{
        paramsMap.put("s_id", this.getCurrentUser(request).getS_id());
        Map<String, Object> resultMap = ownerDao.selectThingsIdIsBind(paramsMap);
        if (resultMap == null){
            return new ResponseBean(Constants.CODE_SUCCESS,"物联网号不存在！",false);
        }else if((Integer) resultMap.get("register_type") == 1){
            return new ResponseBean(Constants.CODE_SUCCESS,"物联网号未注册！",true);
        }else {
            return new ResponseBean(Constants.CODE_SUCCESS, "物联网号已注册！", false);
        }
    }


    @PostMapping("/addCarOwerBindThingsId")
    public ResponseBean addCarOwerBindThingsId(@RequestBody Map<String,Object> paramsMap, HttpServletRequest request)throws Exception{
        paramsMap.put("s_id", getCurrentUser(request).getS_id());
        paramsMap.put("update_user", getCurrentUser(request).getId());
        paramsMap.put("create_user", getCurrentUser(request).getId());
        paramsMap.put("user_name", getCurrentUser(request).getUser_name());
        paramsMap.put("o_id", paramsMap.get("id"));
        paramsMap.put("id", UuidUtil.get32UUID());// 车辆表主键id
        /*String thingsId = paramsMap.get("things_id").toString();
        int selectThingsId = ownerDao.selectThingsId(thingsId);
        if (selectThingsId > 0){
            throw  new MyException(Constants.CODE_HANDEL_FAILED,"该物联网号编号已被绑定");
        }*/
        // 1车辆表插入数据
        int result = ownerDao.addCar(paramsMap);
        // 2车主_车辆关联插入数据
        paramsMap.put("c_id", paramsMap.get("id"));// 将车辆id 保存为c_id
        paramsMap.put("id", UuidUtil.get32UUID()); // 关联表的id
        result += ownerDao.addOwnerCar(paramsMap);
        // 3将关联的物联网卡设置为已绑定
        paramsMap.put("register_type", 0);
        result += ownerDao.updateThings(paramsMap);
        if(result > 0){
            return new ResponseBean(Constants.CODE_SUCCESS,"绑定成功！",null);
        }
        else{
            return new ResponseBean(Constants.CODE_ERROR,"绑定失败！",null);
        }
    }

    /**
     * 消息中心-查看-导出-根据消息ID导出即将过期的物联网关号
     */
    @GetMapping("/exportRenewalMessage")
    public void exportRenewalMessage(@RequestParam String id, HttpServletResponse response) throws Exception {
        List<RenewalCardEntity> info = carDataService.exportRenewalMessage(id);
        ExcelUtils.exportExcelToTarget(response, "到期续费物联网卡列表:" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()), info, RenewalCardEntity.class);
    }
}
